const fs = require("fs-extra");
const csv = require("csv-parser");
const knows=['长沙','华宁']

function getCSVData(file = "../PayPal2025.1.1-3.31.CSV") {
  const results = [];
  const name=file.split("/").pop()
  const channel =knows.find(item=>name.includes(item))||name.slice(0, 2) || "";
  return new Promise((resolve, reject) => {
    fs.createReadStream(file)
      .pipe(csv()) // 解析 CSV 数据
      .on("data", (row) => {
        results.push({ ...row, '来源':channel }); // 逐行读取
      })
      .on("end", () => {
        console.log(`${file}读取完成，结果：`, results.length, "行");
        // console.log(results.slice(0, 10));
        resolve(results);
      })
      .on("error", (err) => {
        console.error("读取错误:", err);
        reject(err);
      });
  });
}
module.exports = {
  getCSVData,
};
